package com.controller;


import com.dto.MaterialInputSearchForm;
import com.entity.Employee;
import com.entity.MaterialInput;
import com.result.Result;
import com.service.MaterialInputService;
import com.service.OrdersService;
import com.service.SupplierService;
import com.utils.PageObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>
 *  物料输入前端控制器
 * </p>
 *
 * @author aiou
 * @since 2025-06-08
 */
@Controller
@RequestMapping("/materialInput")
public class MaterialInputController {

    @Autowired
    private MaterialInputService materialInputService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private OrdersService ordersService;

    //Excel导入接口
    @PostMapping("/import")
    public String materialInputImport(@RequestParam("file")MultipartFile file,Model model) throws IOException {
        Result excelImport = materialInputService.excelImport(file.getInputStream());
        //导入成功，则重定向到列表接口，1是Result中定义的成功的Code
        if (excelImport.getCode().equals(1)) return "redirect:/materialInput/list";
        //导入失败，向前端传报错信息，并跳转到materialInput页面
        model.addAttribute("errorMsg", excelImport.getMsg());
        return "materialInput";
    }

    //物料输入查询接口
    //分页查询用@GetMapping，Supplier查询用@PostMapping，所以综合用@RequestMapping
    @RequestMapping("/list")
    public String list(Model model, PageObject pageObject,MaterialInputSearchForm materialInputSearchForm) {
        //Model：SpringMVC提供的数据模型，用于封装数据，传递给页面
        //"list"：前端页面访问该数据的名称
//        model.addAttribute("list",materialInputService.list(pageObject));
        PageObject<MaterialInput> resultPage = materialInputService.list(pageObject,materialInputSearchForm);
//        model.addAttribute("list", resultPage.getData());
        //给前端传分页查询数据,list包含在PageObject中
        model.addAttribute("page", resultPage);
        //给前端传供应商数据，供下拉框使用
        model.addAttribute("supplierList", supplierService.list());
        model.addAttribute("form", materialInputSearchForm);
        //请求转发到materialInput页面
        return "materialInputList";
    }

    //Excel导出接口
    @GetMapping("/export")
    public void exportData(HttpServletResponse response) {
        materialInputService.excelOutput(response);
    }

    //审核和入库
    @PostMapping("/verify")
    @ResponseBody
    public String verify(Integer status, String idArray) {
        boolean verify = materialInputService.verify(status, idArray);
        if (verify) return "success";
        return "fail";
    }

    //删除
    @PostMapping("/delete")
    @ResponseBody
    public String delete(String idArray){
        boolean delete = this.materialInputService.delete(idArray);
        if(delete) return "success";
        return "fail";
    }

    //入库
    @PostMapping("/inStorage")
    @ResponseBody
    public String inStorage(String idArray, HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        boolean inStore = ordersService.inStore(idArray,employee.getEmployeeName());
        if(!inStore) return "fail";
        return "success";
    }



}

